package com.example.mci.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class StartActivity extends Activity implements OnInitListener {
	private TextToSpeech myTTS;

	// voice recognition and general variables
	// variable for checking Voice Recognition support on user device
	private static final int VR_REQUEST = 999;
	// Log tag for output information
	private final String LOG_TAG = "SpeechRepeatActivity";// ***enter your own
															// tag here***
	// TTS variables
	// variable for checking TTS engine data on user device
	private int MY_DATA_CHECK_CODE = 0;
	// Text To Speech instance
	private TextToSpeech repeatTTS;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		Intent checkTTSIntent = new Intent();
		checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkTTSIntent, 0);
		ImageView i1 = (ImageView) findViewById(R.id.map_view);

		/*SVG svg = SVGParser.getSVGFromResource(getResources(), R.drawable.map);
		Drawable pic = svg.createPictureDrawable();
		i1.setImageDrawable(pic);*/

		Button b1 = (Button) findViewById(R.id.speak);
		b1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				speak("Was wollen Sie tun?");
			}

		});

		Button b2 = (Button) findViewById(R.id.recognize);

		// find out whether speech recognition is supported
		PackageManager packManager = getPackageManager();
		List<ResolveInfo> intActivities = packManager.queryIntentActivities(
				new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
		if (intActivities.size() != 0) {
			// speech recognition is supported - detect user button clicks
			b2.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					listenToSpeech();
				}

			});
		} else {
			// speech recognition not supported, disable button and output
			// message
			b2.setEnabled(false);
			Toast.makeText(this, "Oops - Speech recognition not supported!",
					Toast.LENGTH_LONG).show();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_start, menu);
		return true;
	}

	private void speak(String s) {
		myTTS.speak(s, TextToSpeech.QUEUE_FLUSH, null);
	}

	public void onInit(int initStatus) {
		if (initStatus == TextToSpeech.SUCCESS) {
			myTTS.setLanguage(Locale.GERMAN);

		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				myTTS = new TextToSpeech(this, this);
			} else {
				Intent installTTSIntent = new Intent();
				installTTSIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installTTSIntent);
			}
		}
		if (requestCode == VR_REQUEST && resultCode == RESULT_OK) {
			// store the returned word list as an ArrayList
			ArrayList<String> suggestedWords = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			// set the retrieved list to display in the ListView using an
			// ArrayAdapter
			if (data!=null)
				speak(suggestedWords.get(0));
			

		}else {if(requestCode==VR_REQUEST){
			speak("Ich habe Sie leider nicht verstanden! Bitte wiederholen Sie.");
			try {
				Thread.sleep(3200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			listenToSpeech();
		}}
		

		// tss code here
		// call superclass method
		super.onActivityResult(requestCode, resultCode, data);

	}

	private void listenToSpeech() {
		// start the speech recognition intent passing required data
		Intent listenIntent = new Intent(
				RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		// indicate package
		listenIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
				getClass().getPackage().getName());
		// message to display while listening
		listenIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word!");
		// set speech model
		listenIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		// specify number of results to retrieve
		listenIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
		// start listening
		startActivityForResult(listenIntent, VR_REQUEST);
	}

}
